﻿/******************************************************    
文件名:TableHeatTreatingDataDB.cs
版权  (c)  2010-11-23 淘渺圈圈工作室
作者:黄媛媛
创建日期:2010-12-05
维护人员:黄媛媛
维护日期:2010-12-05
文件功能描述:金属材料热处理网络学习平台_访问TableHeatTreatingData数据表操作
******************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Data;

namespace Mmht.DB
{
    /// <summary>
    /// 热处理数据
    /// </summary>
    class TableHeatTreatingDataDB
    {
        operate op = new operate();

        public string UpdateHeatTreatingData(DataTable dt, int create_by)
        {
            clearAll();
            int success = 0;
            int failure = 0;
            string message_s = string.Empty;
            string message_f = "第";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                if (insertItem(row, create_by))
                {
                    success++;
                }
                else
                {
                    failure++;
                    message_f += i.ToString() + ",";
                }
            }
            message_s = "热处理数据表共更新数据" + success.ToString() + "条,共计更新失败" + failure.ToString() + "条。";
            if (failure > 0)
            {
                message_f = message_f.Substring(0, message_f.Length - 1) + "行数据更新失败。";
                return message_s + message_f;
            }
            else
            {
                return message_s;
            }
        }

        private bool clearAll()
        {
            string sql = @"DELETE FROM heat_treating_data";
            return op.Execute(sql);
        }

        private bool deleteItem(string key)
        {
            string sql = @"DELETE FROM heat_treating_data WHERE name='" + key + "'";
            return op.Execute(sql);
        }

        private bool insertItem(DataRow row, int create_by)
        {
            string fold = "files/heat_treating_data/";
            HeatTreatingData h = new HeatTreatingData();
            h.name = row[0].ToString();
            h.number = row[1].ToString();
            h.a1 = row[2].ToString();
            h.a3 = row[3].ToString();
            h.ms_mz = row[4].ToString();
            h.ttt_curve = fold + row[5].ToString();
            h.cct_curve = fold + row[6].ToString();
            h.jominy_curve = fold + row[7].ToString();
            h.tempering_curve = fold + row[8].ToString();
            h.diagram = fold + row[9].ToString();
            h.technics = row[10].ToString();
            h.remark = row[11].ToString();
            h.create_by = create_by;
            string sql = @"INSERT INTO heat_treating_data
             (name, number, a1, a3, ms_mz, ttt_curve, cct_curve, jominy_curve, 
              tempering_curve, diagram, technics, remark,viewnum,created_at,created_by) VALUES
             ('" + h.name + "','" + h.number + "','" + h.a1 + "','" + h.a3 + "','" + h.ms_mz + "','" + 
                 h.ttt_curve + "','" + h.cct_curve + "','" + h.jominy_curve + "','" + 
                 h.tempering_curve + "','" + h.diagram + "','" + h.technics + "','" + 
                 h.remark + "','" + h.viewnum.ToString() + "','" + h.create_at + "','" + 
                 h.create_by.ToString() + "')";
            return op.Execute(sql);
        }

        private class HeatTreatingData
        {
            public string name, number, a1, a3, ms_mz, ttt_curve, cct_curve, jominy_curve, tempering_curve, diagram, technics, remark;
            public int create_by;
            public int viewnum = 0;
            public DateTime create_at = DateTime.Now;
        }
    }
}
